import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Login {
    private final static String URL = "jdbc:mysql://localhost:3306/usersdb?useSSL=false&serverTimezone=UTC";
    private final static String ROOT = "root";
    private final static String PASSWORD = "123456";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("用户管理系统");
            System.out.println("1. 登录");
            System.out.println("2. 注册");
            System.out.println("3. 删除用户");
            System.out.println("4. 修改密码");
            System.out.println("5. 退出系统");
            System.out.print("请选择操作：");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    login(scanner);
                    break;
                case 2:
                    register(scanner);
                    break;
                case 3:
                    deleteUserById(scanner);
                    break;
                case 4:
                    updatePasswordById(scanner);
                    break;
                case 5:
                    System.out.println("感谢使用，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
        }
    }

    public static void login(Scanner scanner) {
        System.out.println("请输入用户名：");
        String name = scanner.next();
        System.out.println("请输入密码：");
        String pwd = scanner.next();

        try (Connection connection = DriverManager.getConnection(URL, ROOT, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement("select * from users where username =? and password =?;")) {

            preparedStatement.setString(1, name);
            preparedStatement.setString(2, pwd);
            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                System.out.println("登录成功");
            } else {
                System.out.println("登录失败");
            }
        } catch (SQLException e) {
            System.out.println("数据库操作异常：" + e.getMessage());
        }
    }


    public static void register(Scanner scanner) {
        System.out.println("请输入注册用户名：");
        String name = scanner.next();
        System.out.println("请输入注册密码：");
        String pwd = scanner.next();

        try (Connection connection = DriverManager.getConnection(URL, ROOT, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement("insert into users(username, password) values (?,?);")) {

            preparedStatement.setString(1, name);
            preparedStatement.setString(2, pwd);
            int affectedRows = preparedStatement.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("注册成功");
            } else {
                System.out.println("注册失败");
            }
        } catch (SQLException e) {
            System.out.println("数据库操作异常：" + e.getMessage());
        }
    }


    public static void deleteUserById(Scanner scanner) {
        System.out.println("请输入要删除用户的 ID：");
        int id = scanner.nextInt();

        try (Connection connection = DriverManager.getConnection(URL, ROOT, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement("delete from users where id =?;")) {

            preparedStatement.setInt(1, id);
            int affectedRows = preparedStatement.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("用户删除成功");
            } else {
                System.out.println("未找到对应 ID 的用户，删除失败");
            }
        } catch (SQLException e) {
            System.out.println("数据库操作异常：" + e.getMessage());
        }
    }

    public static void updatePasswordById(Scanner scanner) {
        System.out.println("请输入要修改密码的用户 ID：");
        int id = scanner.nextInt();
        System.out.println("请输入新密码：");
        String newPwd = scanner.next();

        try (Connection connection = DriverManager.getConnection(URL, ROOT, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement("update users set password =? where id =?;")) {

            preparedStatement.setString(1, newPwd);
            preparedStatement.setInt(2, id);
            int affectedRows = preparedStatement.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("密码修改成功");
            } else {
                System.out.println("未找到对应 ID 的用户，修改失败");
            }
        } catch (SQLException e) {
            System.out.println("数据库操作异常：" + e.getMessage());
        }
    }
}